package cn.itheima.action.takedelivery;

import cn.itheima.action.common.BaseAction;
import cn.itheima.domain.take_delivery.WayBill;
import cn.itheima.service.takedelivery.WayBillService;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by millions on 2017/7/10.
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class WayBillAction extends BaseAction<WayBill> {

    @Autowired
    private WayBillService wayBillService;

    @Action(value = "waybill_pageQuery", results = {@Result(type = "json")})
    public String waybillPageQuery() {

        Pageable pageable = new PageRequest(page - 1, rows);

        Page<WayBill> pageData = wayBillService.pageQuery(pageable);
        pushPageDataToValueStack(pageData);
        return SUCCESS;
    }

    @Action(value = "waybill_save", results = {@Result(type = "json")})
    public String waybillSave() {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            wayBillService.save(model);
            // 保存成功
            result.put("success", true);
            result.put("msg", "保存运单成功！");

        } catch (Exception e) {
            e.printStackTrace();
            // 保存失败
            result.put("success", false);
            result.put("msg", "保存运单失败！");
        }
        ActionContext.getContext().getValueStack().push(result);
        return SUCCESS;
    }

    @Action(value = "waybill_findByWayBillNum", results = { @Result(name = "success", type = "json") })
    public String findByWayBillNum() {
        // 调用业务层 查询
        WayBill wayBill = wayBillService
                .findByWayBillNum(model.getWayBillNum());

        ActionContext.getContext().getValueStack().push(wayBill);

        return SUCCESS;
    }

}
